﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using DevComponents.DotNetBar;
using KP_CommonLibrary;
using KP_CommonLibrary.KP_DataAccess;

namespace KEEPER_BRR_13dotNET.BusinessLogic
{
    public class KP_Menu
    {
        #region Variable
        DataTable _dtmenu;
        #endregion

        #region Properties
        public DataTable DtMenu
        {
            get { return _dtmenu; }
            set { _dtmenu = value; }
        }
        #endregion

        #region Constructors
        public KP_Menu()
        {
            _dtmenu = new DataTable();
        }
        #endregion

        #region Methods

        #region Load_DataTableMenu

        public void Load_DataTableMenu()
        {
            try
            {
                _dtmenu = KP_DataAccess.Get_DataTable("SELECT * FROM tblSysMenu ORDER BY Menu_Id, Menu_Id0");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Gen_Toolstrip
        public void Gen_Toolstrip(Bar toolstrip)
        {
            DataRow[] drtoolstrip = DtMenu.Select("On_Bar <> ''");
            foreach (DataRow row in drtoolstrip)
            {
                ButtonItem tsbtn = new ButtonItem();
                tsbtn.Tag = row;
                tsbtn.Name = "tbbi" + row["Menu_Id"].ToString();
                tsbtn.Text = row["Bar_Name"].ToString();
                tsbtn.ButtonStyle = eButtonStyle.ImageAndText;
                tsbtn.Image = GetImage(row);
                tsbtn.Click += new EventHandler(Exec_Menu_Click);
                toolstrip.Items.Add(tsbtn);
            }

            if (toolstrip.Items.Count == 0)
                toolstrip.Visible = false;
            toolstrip.RecalcLayout();

        }
        #endregion

        #region Gen_Treeview
        public void Gen_Treeview(DevComponents.AdvTree.AdvTree advTree)
        {
            DataRow[] topMenu = DtMenu.Select("Menu_Id0=''");
            foreach (DataRow node in topMenu)
            {
                string key = node["Menu_Id"].ToString();
                DevComponents.AdvTree.Node menuNode = new DevComponents.AdvTree.Node(node["Bar_Name"].ToString());
                menuNode.Tag = node;
                advTree.Nodes.Add(menuNode);
                if (!node.IsNull("Image"))
                {
                    if (System.IO.File.Exists(Application.StartupPath + "\\Image\\" + node["Image"].ToString()))
                    {
                        Image img = Image.FromFile(Application.StartupPath + "\\Image\\" + node["Image"].ToString());
                        menuNode.Image = img;
                    }
                }
                BuildChildMenuTree(menuNode, key);
            }
        }

        void BuildChildMenuTree(DevComponents.AdvTree.Node parrentNode, string parrentId)
        {
            DataRow[] topMenu = DtMenu.Select("Menu_Id0='" + parrentId + "'");

            foreach (DataRow node in topMenu)
            {
                string key = node["Menu_Id"].ToString();
                DevComponents.AdvTree.Node menuNode = new DevComponents.AdvTree.Node(node["Bar_Name"].ToString());
                menuNode.Tag = node;
                parrentNode.Nodes.Add(menuNode);
                if (!node.IsNull("Image"))
                {
                    if (System.IO.File.Exists(Application.StartupPath + "\\Image\\" + node["Image"].ToString()))
                    {
                        Image img = Image.FromFile(Application.StartupPath + "\\Image\\" + node["Image"].ToString());
                        menuNode.Image = img;
                    }
                }
                BuildChildMenuTree(menuNode, key);
            }
        }
        #endregion

        #region Gen_Menu
        public void Gen_Menu(Bar menubar)
        {
            int i = -1;
            string key = "";

            foreach (DataRow row in DtMenu.Select("Menu_Id0=''"))
            {
                if (row["Menu_Id0"].ToString().Trim() == "")
                {
                    key = row["Menu_Id"].ToString();

                    ButtonItem item = new ButtonItem("bi" + key, row["Bar_Name"].ToString());
                    menubar.Items.Add(item);
                    BuildSubmenu(item, key);
                    i++;
                }
            }
            menubar.RecalcLayout();
            menubar.Invalidate();
        }

        protected void BuildSubmenu(ButtonItem parent, string id)
        {
            DataRow[] childrenRows = DtMenu.Select("Menu_Id0='" + id + "'");
            string group = "";
            bool beginGroup = false;
            foreach (DataRow node in childrenRows)
            {
                if (group == "")
                    group = node["Bar_Type"].ToString();
                else
                {
                    if (group != node["Bar_Type"].ToString())
                    {
                        beginGroup = true;
                        group = node["Bar_Type"].ToString();
                    }
                    else
                        beginGroup = false;
                }

                ButtonItem item = new ButtonItem("bi" + node["Menu_Id"].ToString(), node["Bar_Name"].ToString());
                item.Tag = node;
                item.Click += new EventHandler(Exec_Menu_Click);
                item.BeginGroup = beginGroup;
                item.Image = GetImage(node);
                parent.SubItems.Add(item);

                BuildSubmenu(item, node["Menu_Id"].ToString());
            }

        }
        #endregion
        public Image GetImage(DataRow row)
        {
            if (!row.IsNull("Image"))
            {
                if (System.IO.File.Exists(Application.StartupPath + "\\Image\\" + row["Image"].ToString()))
                    return Image.FromFile(Application.StartupPath + "\\Image\\" + row["Image"].ToString());
            }
            return null;
        }
        #region Exit_App
        public static void Exit_App()
        {
            Application.ExitThread();
        }

        #endregion

        void Exec_Menu_Click(object sender, EventArgs e)
        {
            ButtonItem mnItem = sender as ButtonItem;
            DataRow row = mnItem.Tag as DataRow;
            AppContext.MainForm.PerformCommandClick(sender, row);
        }

        #endregion
    }   
}
